package product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ObjectUtils;
import product.domain.SkuInfo;
import product.domain.SpuInfo;
import product.service.SkuInfoService;
import product.mapper.SkuInfoMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author panghu
 * @description 针对表【pms_sku_info(sku信息)】的数据库操作Service实现
 * @createDate 2023-03-04 21:03:08
 */
@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo>
        implements SkuInfoService {

    @Override
    public Page<SkuInfo> querPage(Map<String, Object> params) {
        LambdaQueryWrapper<SkuInfo> queryWrapper = getQueryWrapper(params);
        Page<SkuInfo> skuInfoPage = new Page<>(Integer.parseInt((String) params.get("page")), Integer.parseInt((String) params.get("limit")));
        return this.baseMapper.selectPage(skuInfoPage, queryWrapper);
    }

    @Override
    public List<SkuInfo> getListByCatId(Long catId) {
        if (catId == 0L) {
            return this.baseMapper.selectList(null);
        }
        return this.baseMapper.selectList(new LambdaQueryWrapper<SkuInfo>().eq(SkuInfo::getCatalogId, catId));
    }

    private LambdaQueryWrapper<SkuInfo> getQueryWrapper(Map<String, Object> params) {
        LambdaQueryWrapper<SkuInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!ObjectUtils.isEmpty(params.get("key")), SkuInfo::getSkuName, params.get("key")).or().eq(!ObjectUtils.isEmpty(params.get("key")), SkuInfo::getSkuId, params.get("key"));
        wrapper.eq(!ObjectUtils.isEmpty(params.get("catelogId")), SkuInfo::getCatalogId, params.get("catelogId"));
        wrapper.eq(!ObjectUtils.isEmpty(params.get("brandId")), SkuInfo::getBrandId, params.get("brandId"));
        if (!ObjectUtils.isEmpty(params.get("min"))) {
            BigDecimal min = new BigDecimal(params.get("min").toString());
            if (min.compareTo(new BigDecimal("0")) > 0) {
                wrapper.ge(SkuInfo::getPrice, min);
            }
        }
        if (!ObjectUtils.isEmpty(params.get("max"))) {
            BigDecimal max = new BigDecimal(params.get("max").toString());
            if (max.compareTo(new BigDecimal("0")) > 0) {
                wrapper.ge(SkuInfo::getPrice, max);
            }
        }
        return wrapper;
    }
}




